import logging

logger = logging.getLogger(__name__)
logger.setLevel(level=logging.INFO) # 级别
handler = logging.FileHandler("log.txt",encoding='utf-8')
handler.setLevel(logging.INFO)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)
console = logging.StreamHandler()
console.setLevel(logging.INFO)
logger.addHandler(handler)
logger.addHandler(console)

if __name__ == '__main__':
    logger.info("Start print log") # 比较详细的
    logger.debug("Do something") # 只有debug
    logger.warning("Something maybe fail.") # 警告
    logger.info("Finish")